package o;

import android.content.Context;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import o.cof.a;

public final class cph {
    private static final Object ˎ = new Object();
    private static cph ˏ;
    private final ThreadPoolExecutor ˊ;
    public byte[] ˋ = new byte[0];
    private final Context ॱ;

    public static cph ॱ(Context context) {
        cph o_cph;
        synchronized (ˎ) {
            if (ˏ == null) {
                ˏ = new cph(context);
            }
            o_cph = ˏ;
        }
        return o_cph;
    }

    private cph(Context context) {
        this.ॱ = context;
        this.ˊ = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new cqd("PackageTaskList"));
    }

    public ManagerTask ˋ(String str, int i, cnz o_cnz, int i2, int i3, long j, cod... o_codArr) {
        synchronized (this.ˋ) {
            ManagerTask ˏ = cpa.ˊ().ˏ(str, o_codArr);
            if (ˏ == null) {
                return null;
            } else if ((ˏ.mode == 2 || ˏ.mode == 1002) && i3 != 3 && i3 != 5) {
                return null;
            } else if (j == 0 || ˏ.taskId == j) {
                cnu.ˏ.ˋ("PackageManagerProcessListManager", " notifyNextTask, current:" + str + ",taskId:" + j + ",from:" + i3);
                ˏ.status = o_cnz;
                cpf.ˋ(this.ॱ).ॱ(i, ˏ, i2);
                cpa.ˊ().ˎ();
                cpc.ˎ(this.ॱ).ˎ(ˏ.taskId);
                this.ˋ.notifyAll();
                return ˏ;
            } else {
                cnu.ˏ.ˏ("PackageManagerProcessListManager", "receive the error taskId" + str + ",taskId:" + j);
                return null;
            }
        }
    }

    public static void ˋ(Context context, ManagerTask managerTask, String str) {
        cpk.ˏ().ˎ(System.currentTimeMillis());
        Thread thread = new Thread(new cpj(context, managerTask));
        thread.setName(str);
        thread.start();
    }

    private void ॱ() {
        int ॱ = cpa.ˊ().ॱ();
        ManagerTask ˋ = cpa.ˊ().ˋ();
        if (ˋ != null) {
            cnu.ˏ.ˎ("PackageManagerProcessListManager", " leftList size:" + ॱ);
            ˋ(ˋ);
        }
        if (cpa.ˊ().ॱ() <= 0) {
            cpk.ˏ().ˎ(0);
            cox.ˋ(this.ॱ).ˎ(this.ॱ);
        }
    }

    private void ˋ(ManagerTask managerTask) {
        synchronized (this.ˋ) {
            cpa.ˊ().ॱ(managerTask);
            String str = "unknow process type";
            if (cod.INSTALL == managerTask.processType) {
                str = "install|pkg:" + managerTask.packageName + "|target:" + ((a) managerTask.apkInfos.get(0)).ˏ + "|flag:" + managerTask.flag;
            } else if (cod.UNINSTALL == managerTask.processType) {
                str = "uninstall|pkg:" + managerTask.packageName + "|flag:" + managerTask.flag;
            } else if (cod.INSTALL_EXISTING_PKG == managerTask.processType) {
                str = "installExist|pkg:" + managerTask.packageName + "|flag:" + managerTask.flag;
            }
            try {
                ˋ(this.ॱ, managerTask, str);
                this.ˋ.wait();
            } catch (Throwable e) {
                cnu.ˏ.ˊ("PackageManagerProcessListManager", "wait the processing lock failed!!!", e);
            }
            if (cop.ॱ != null) {
                cop.ॱ.ˎ();
            }
        }
    }

    public void ˏ() {
        cnu.ˏ.ˋ("PackageManagerProcessListManager", " startTask");
        this.ˊ.execute(new Runnable(this) {
            final /* synthetic */ cph ˊ;

            {
                this.ˊ = r1;
            }

            public void run() {
                this.ˊ.ॱ();
            }
        });
    }
}
